<template>
  <div class="com-zhou">
    <div class="title">
      <span>{{ "▎ " + showTitle }}</span>
      <span class="iconfont title-icon" @click="showChoice = !showChoice"
        >></span
      >
      <div class="select-con" v-show="showChoice">
        <div
          class="select-item"
          v-for="item in selectTypes"
          :key="item.key"
          @click="handleSelect(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  data() {
    return {
      chartInstane: null,
      allData: null, // 从服务器中获取的所有数据
      showChoice: false, // 是否显示可选项
      choiceType: "chiwanfan", // 显示的数据类型
      startValue: 0, // 区域缩放的起点值
      endValue: 6, // 区域缩放的终点值
    };
  },
  mounted() {
    this.initChart();
    this.getData();  
    this.screenAdapter();
  },

  computed: {
    selectTypes() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type.filter((item) => {
          return item.key !== this.choiceType;
        });
      }
    },
    showTitle() {
      if (!this.allData) {
        return "";
      } else {
        return this.allData[this.choiceType].title;
      }
    },
   
  },
  methods: {
    initChart() {
      this.chartInstance = echarts.init(this.$refs.trend_ref);
      const initOption = {
        grid: {
          top: "22%",
          left: "8%",
          right: "5%",
          bottom: "40%",
          containLabel: false,
        },
        tooltip: {
          show: true,
        },
        xAxis: [
          {
            type: "category",
          },
        ],
        yAxis: {
          type: "value",
          axisLabel: {
            textStyle: {
              color: "#fff",
            },
            interval: 0,
          },
        },
        series: [
          {
            type: "bar",
              itemStyle: {
              barBorderRadius: [9, 9, 0, 0],
            },
          },
        ],
      };
      // console.log(this.chartInstane)
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      // await this.$http.get()
      // 对allData进行赋值
      const { data: ret } = await this.$http.get(
        "http://localhost:8080/dangexingwei.json"
      );
      this.allData = ret;
      console.log(this.allData);
      // console.log(this.allData[this.choiceType].data.length)
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      const colorArr = {
        chiwanfan: "#0ba82c",
        chixiawucha: "#2c6eff",
        chiyexiao: "#16f2d9",
        chizaocan: "#fe211e",
        chizaowufan: "#fa6900",
        chizhongcan: "#eedd78",
        jieyue: "#73a373",
        jinrutushuguan: "#73b9bc",
        rixiaobaihuo: "#7289ab",
        shangke: "#91ca8c",
        shiwubanli: "#f49f42",
        zixihuokeyan: "#0ba82c",
        xiuxi: "#2c6eff",
      };
      const valueArr = this.allData[this.choiceType].data;
      // console.log(colorArr.chiwanfan)
      const name = valueArr.map((item) => {
        return item.name;
      });
      // 所有省份对应的销售金额
      const value = valueArr.map((item) => {
        return item.value;
      });
      // console.log(name)
      // console.log(value)
      const dataOption = {
        color: colorArr[this.choiceType],
        xAxis: {
          data: name,
          axisLabel: {
            textStyle: {
              color: "#fff",
            },
          },
        },
        dataZoom: {
          show: true,
          id: "dataZoomX",
          type: "slider",
          xAxisIndex: [0],
          filterMode: "empty",
          startValue: this.startValue,
          endValue: this.endValue,
          textStyle: {
            color: "#fff",
          },
        },
        series: [
          {
            data: value,
              itemStyle: {
              barBorderRadius: [9, 9, 0, 0],
            },
          },
        ],
      };
      this.$nextTick(() => {
        this.chartInstance.setOption(dataOption);
      });
    },
    screenAdapter() {
      
      const adapterOption = {
        legend: {
          itemWidth: 18,
          itemHeight:18,
          itemGap: 18,
          textStyle: {
            fontSize: 9,
          },
        },
        series: [
          {
            barWidth: 18,
            itemStyle: {
              barBorderRadius: [9, 9, 0, 0],
            },
          },
        ],
      };
      this.$nextTick(() => {
        this.chartInstance.setOption(adapterOption);
      });
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > 19) {
          this.startValue = 0;
          this.endValue = 6;
        }
        this.updateChart();
      }, 2000);
    },
    handleSelect(currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    },
  },
};
</script>

<style scoped>
.com-zhou {
    background-color: rgba(0, 0, 0, 0.3);
  width: 100%;
  height: 100%;
}
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  font-size: 18px;
  color: white;
}
.title-icon {
  margin-left: 10px;
  cursor: pointer;
}
.select-con {
  background-color: #222733;
  font-size: 18px;
}
.com-chart {
  width: 500px;
  height: 300px;
}
</style>
